package com.ruitu.mall_98.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98.R;
import com.ruitu.router_module.bean.EventModel;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private IWXAPI iwxapi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxpay_entry);
        iwxapi = WXAPIFactory.createWXAPI(this, Config.APP_ID);
        iwxapi.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        iwxapi.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        Logg.i("baseReq: " + baseReq);
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Logg.i("OpenId :" + baseResp.openId);
        Logg.i("Transaction :" + baseResp.transaction);

        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (baseResp.errCode) {
                case 0:
                    ToastUtils.showShort("支付成功");
                    Logg.i("支付成功");
                    Arad.bus.post(new EventModel(18120500));
                    finish();
                    break;
                case -1:
                    Logg.i("Transaction :" + baseResp.errStr);
                    AlertDialog builder = new AlertDialog.Builder(this).create();
                    builder.setTitle(R.string.app_tip);
                    builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(baseResp.errCode)));
                    builder.show();
                    break;
                case -2:
                    ToastUtils.showShort("支付已取消");
                    finish();
                    break;
            }

            AlertDialog builder = new AlertDialog.Builder(this).create();
            builder.setTitle(R.string.app_tip);
            builder.setMessage(getString(R.string.pay_result_callback_msg,String.valueOf(baseResp.errCode)));
            builder.show();
        }
    }


}
